import http from '@/server/request'
import * as Utils from '@/utils/index.js'

let header = { 'content-type': 'application/json' }
let token_type = 'bearer'
let acc_token = ''
let acc_token_header_key = 'Authorization'
let baseUrl = 'https://api.wxls.pro/topic'

/**
 * 获取优惠券列表
 * @param {Number} "commodityType"	商品类型0优惠券1实物
 * @param {Number} "couponDistributionType"	配送方式0线下自提1其他
 * @param {Number} "couponGainType"	兑换方式0免费1积分2支付
 * @param {String} "couponName"	优惠券名称
 * @param {Number} "shopId"	店铺id
 */
export const getCouponList = async (params) => {
  let header = {}
  const token = uni.getStorageSync('wxlsToken')
  header[acc_token_header_key] = `${token_type} ${token}`
  const data = await http.request({
		baseUrl,
    url: '/api/coupon/coupon',
    method: 'GET',
    header,
    data: params
  })
  if (data.data.code === '401') {
    uni.showToast({
      title: '登录过期',
      icon: 'none'
    })
    Utils.appInteractionH5.tologin.toAppLogin()
    return
  }
  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '获取优惠券失败',
      duration: 2000,
      icon: 'none'
    })
    return Promise.reject(new Error(data.data.message || '获取优惠券失败'))
  }
  return data
}

/**
 * 领取优惠券
 * @param {Number} "couponId"	优惠券ID
 */
export const gainCoupon = async (params) => {
  let header = { encryption: true }
  const token = uni.getStorageSync('wxlsToken')
  header[acc_token_header_key] = `${token_type} ${token}`
  const data = await http.request({
		baseUrl,
    url: '/api/coupon/gainCoupon',
    method: 'POST',
    header,
    data: params
  })
  if (data.data.code === '401') {
    uni.showToast({
      title: '登录过期',
      icon: 'none'
    })
    Utils.appInteractionH5.tologin.toAppLogin()
    return
  }
	if (data.data.code === '5002' || (data.data.code === '5001' && (data.data.message.includes('结束') || data.data.message.includes('未开始')))) {
    // 5002没库存  "5001"活动未开始/已经达到领取上限(包括本日)/本次活动已结束
	  return Promise.reject(data.data)
	}
  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '领取优惠券失败',
      duration: 2000,
      icon: 'none'
    })
    return Promise.reject(new Error(data.data.message || '领取优惠券失败'))
  }
  return data
}

/**
 * 已领取优惠券列表
 * @param {Number} "id"	id
 * @param {Number} "couponUsed"	是否使用0否1是
 * @param {Number} "shopCouponId"	优惠券id
 * @param {Number} "shopId"	店铺id
 * @param {Number} "useSign"	用户标记
 */
export const getGainUsedList = async (params) => {
  let header = {}
  const token = uni.getStorageSync('wxlsToken')
  header[acc_token_header_key] = `${token_type} ${token}`
  const data = await http.request({
		baseUrl,
    url: '/api/coupon/gainUsedList',
    method: 'GET',
    header,
    data: params
  })
  if (data.data.code === '401') {
		uni.showToast({
		  title: '登录过期',
		  icon: 'none'
		})
    Utils.appInteractionH5.tologin.toAppLogin()
    return
  }
  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '获取已领取优惠券信息失败',
      duration: 2000,
      icon: 'none'
    })
    return Promise.reject(
      new Error(data.data.message || '获取已领取优惠券信息失败')
    )
  }
  return data
}
